﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WindowsFormsApplication2;

namespace Anata_Client.Model
{
    class TransaksiPembayaran
    {
        public int ID_Transaksi;
        DateTime TanggalMasuk;
        DateTime TanggalKeluar;
        int ID_Konsumen;
        int TotalHarga;
        int Nominal;
        int Persentase;
        int Correction;
        
        public TransaksiPembayaran()
        {
            
        }

        public TransaksiPembayaran(int _ID_Transaksi)
        {
            Database db = new Database();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            string sql = "SELECT * FROM transaksi_pembayaran WHERE ID_Transaksi = '" + _ID_Transaksi + "'";

            db.Open();
            ds = db.ExecuteQuery(sql);
            dt = ds.Tables[0];
            DataRow dr = dt.Rows[0];

            ID_Transaksi = (int)dr[0];
            TanggalMasuk = (DateTime)dr[1];
            
            db.Close();
        }

        public void AddTransaksiPembayaran(int _ID_Transaksi, int _Metode, int _Jumlah)
        {
            Database db = new Database();
            long l;
            string sql = "INSERT INTO transaksi_pembayaran(\"ID_Transaksi\", \"Metode\", \"Jumlah\") VALUES (" + _ID_Transaksi.ToString() + "," + _Metode.ToString() + "," + _Jumlah.ToString() + ");select currval('\"TransaksiPembayaran_ID_TransaksiPembayaran_seq\"')";

            db.Open();
            l = db.ExecuteNonQuery(sql);
            db.Close();

            ID_Transaksi = (int)l;

            System.Console.WriteLine(l.ToString());
        }

        public static int JumlahTerbayar(int _ID_Transaksi)
        {
            Database db = new Database();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            int j = 0;
            string sql = "SELECT * FROM transaksi_pembayaran WHERE \"ID_Transaksi\" = '" + _ID_Transaksi + "'";

            db.Open();
            ds = db.ExecuteQuery(sql);
            dt = ds.Tables[0];
            DataRow dr;

            for (int i = 0; i < dt.Rows.Count; ++i)
            {
                dr = dt.Rows[i];

                j += (int)dr[3];
            }

            db.Close();

            return j;
        }

        public void UpdateTransaksi(int _idTransaksi, int _harga)
        {
            Database db = new Database();
            string sql = "UPDATE transaksi SET \"TotalHarga\" = " + _harga.ToString() + " WHERE \"ID_Transaksi\" = " + _idTransaksi.ToString();

            db.Open();
            db.ExecuteDelete(sql);
            db.Close();
        }

        public void DeleteTransaksi(int _idTransaksi)
        {
            Database db = new Database();
            string sql = "DELETE FROM transaksi WHERE \"ID_Transaksi\" = " + _idTransaksi.ToString();

            db.Open();
            db.ExecuteDelete(sql);
            db.Close();
        }

    }
}
